$(function () {
    // 默认加载部门树
    vm.getDept();
});
var ztree;
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: '01'
        },
        key: {
            url: "nourl",
            name: "name" //name 与 typeName 绑定
        },
    },
    callback: {
        beforeCheck: true,
        beforeClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    vm.q.dcode = treeObj.deptId;
    vm.search();
}

var myChart;
var vm = new Vue({
    el: '#rrapp',
    data: {
        q:{
            month: '', // 统计月份
            dcode: '', // 设备组织CODE
            num:10, // 最多显示设备数
            keyword: 'times'// 故障次数 or 故障时长
        },
        orderFields:[
            {name:'故障次数',value:'times'},{name:'故障时长',value:'duration'}
        ],
        chartTitle:'',
        option: {
            color: ['#4472c4'],
            grid:{
                left: '10%'
            },
            title: {
                text: '故障TOP设备统计',
                left: 'center',
                top: '5%'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: {show: true, type: ['line', 'bar']},
                    dataView: {show: true, readOnly: false},
                    saveAsImage: {show: true}
                },
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: function (params) {
                    var tar = params[1];
                    return tar.name + '<br/>' + tar.seriesName + ' : ' + tar.value;
                }
            },
            xAxis: {
                type: 'value',
                minInterval: 1
            },
            yAxis: {
                type: 'category',
                data: [],
                axisLabel: {
                    rotate:  30
                }
            },
            series: [{
                name:'次数/时长',
                data: [],
                type: 'bar'
            }]
        }
    },
    methods: {
        getDept: function () {
            //加载部门树
            Ajax.request({
                url: '../sys/user/treeDeptBySCXLevel?level=3',
                async: false,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    ztree.expandAll(true);
                }
            });
        },
        search: function () {
            var param = {};
            if(this.q.month){
                param.month = this.convertGMT2Month(this.q.month);
            }
            if(this.q.dcode){
                param.dcode = this.q.dcode;
            }
            if(this.q.num){
                param.topNum = this.q.num;
            }
            param.keyword = this.q.keyword;
            myChart.showLoading();
            // 加载设备状态概览
            Ajax.request({
                url: '../device/stat/topFault',
                type: 'POST',
                params: JSON.stringify(param),
                contentType: 'application/json',
                successCallback: function (res) {
                    myChart.hideLoading();
                    var topFaultDev = res.topFaultDev;
                    var option = {
                        yAxis: {
                            data: topFaultDev.devNames,
                        },
                        series: [{
                            data: topFaultDev.faultTimes,
                        }]
                    };
                    myChart.setOption(option);
                }
            });
        },
        reset: function () {
            this.q = {
                month: '', // 统计月份
                dcode: '', // 设备组织CODE
                num:10, // 最多显示设备数
                keyword: 'times'// 故障次数 or 故障时长
            };
            this.search();
        },
        convertGMT2Month: function(time){
            var date = new Date(time)
            var month = (date.getMonth() + 1);
            return date.getFullYear() + '-' + (month < 10? '0':'') + month;
        }
    },
    mounted: function(){
        // 初始化报表
        myChart = echarts.init(document.getElementById("container"));
        myChart.setOption(this.option,true);
        // 初始化加载数据
        this.search();
    }
});
